package wnhz.com.business.groupshop.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseRecyclerAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.shareshop.bean.HelpCenterBean;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;

/**
 * 信用秘籍
 */
public class CreditCheatsActivity extends BaseActivity implements ActionBarClickListener, BaseRecyclerAdapter.OnDataLoadFinish {


    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.creditCheats_recyclerView)
    RecyclerView creditCheats_recyclerView;

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, CreditCheatsActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credit_cheats);
        ButterKnife.bind(this);

        actionbar.setData("信用秘籍", R.drawable.ic_lefft_white, null, 0, null,0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }

        initData();
    }

    private void initData() {
        Map<String,Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getInfoBean().getToken());
        map.put("goods_type", 4); //4-共享商城规则
        map.put("type", 3); //int   type   1-规则  2-帮助中心  3-信用秘籍
        XUtil.Post(Url.Ucenter_rules, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("---信用秘籍:  ", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.getString("re");
                    if ("1".equals(re)) {
                        Gson gson = new Gson();
                        HelpCenterBean helpCenterBean = gson.fromJson(result, HelpCenterBean.class);
                        List<HelpCenterBean.InfoBean> info = helpCenterBean.getInfo();
                        setData(info);
                    }else if ("-1".equals(jsonObject.optString("re"))){
                        MyToast(getResources().getString(R.string.login_out));
                        MyApplication.getInstance().gotoLoginActivity();
                    }else {
                        MyToast(jsonObject.optString("info"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFinished() {
                super.onFinished();
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private boolean isContentVisibility = true;
    private void setData(final List<HelpCenterBean.InfoBean> info) {
        creditCheats_recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        BaseRVAdapter baseRVAdapter = new BaseRVAdapter(CreditCheatsActivity.this, info) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_help;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                LinearLayout item_help_linearLayout = holder.getView(R.id.item_help_linearLayout);
                holder.setTextView(R.id.item_help_title, info.get(position).getClassname());
                final TextView item_help_content = holder.getTextView(R.id.item_help_content);
                item_help_content.setText(info.get(position).getTwo().get(position).getClassname());

                StringBuffer stringBuffer = new StringBuffer();
                for (int i = 0; i < info.get(position).getTwo().size(); i++) {
                    stringBuffer.append((i+1) + "、" + info.get(position).getTwo().get(position).getClassname() + "\n");
                }

                holder.setTextView(R.id.item_help_content,stringBuffer.toString());
                item_help_linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(isContentVisibility){
                            item_help_content.setVisibility(View.VISIBLE);
                            isContentVisibility = false;
                        }else{
                            item_help_content.setVisibility(View.GONE);
                            isContentVisibility = true;
                        }
                    }
                });
            }
        };
        creditCheats_recyclerView.setAdapter(baseRVAdapter);
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }

    @Override
    public void loadDataFinished(int datasLength) {

    }
}
